//package com.ugoodtech.mdcc.client.controller;
//
///* Copyright (C) Ugoodtech, Inc - All Rights Reserved.
// * Unauthorized copying of this file, via any medium is strictly prohibited.
// * Proprietary and confidential.
// * Written by Stone Shaw.
// */
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.ugoodtech.mdcc.client.dto.JsonResponse;
//import com.ugoodtech.mdcc.client.dto.NotificationMessageResponse;
//import com.ugoodtech.mdcc.client.service.ClientUserService;
//import com.ugoodtech.mdcc.client.service.NotificationMessageService;
//import com.ugoodtech.mdcc.client.service.PushDeviceTokenService;
//import com.ugoodtech.mdcc.client.service.QRService;
//import com.ugoodtech.mdcc.core.Constants;
//import com.ugoodtech.mdcc.core.domain.*;
//import com.ugoodtech.mdcc.core.repository.*;
//import com.ugoodtech.mdcc.core.utils.BaseUtil;
//import com.ugoodtech.mdcc.core.utils.WeChatUtil;
//import io.swagger.annotations.*;
//import org.apache.commons.lang3.StringUtils;
//import org.apache.http.HttpEntity;
//import org.apache.http.HttpResponse;
//import org.apache.http.client.HttpClient;
//import org.apache.http.client.methods.HttpPost;
//import org.apache.http.impl.client.HttpClientBuilder;
//import org.apache.http.util.EntityUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.data.domain.Page;
//import org.springframework.data.domain.PageImpl;
//import org.springframework.data.domain.Pageable;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.crypto.password.PasswordEncoder;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.RestController;
//import springfox.documentation.annotations.ApiIgnore;
//
//import java.io.IOException;
//import java.lang.reflect.InvocationTargetException;
//import java.security.Principal;
//import java.util.*;
//
//@Api(description = "用户相关接口")
//@RestController
//@RequestMapping("/clientUsers")
//public class ClientUserController {
//
//    private final ClientUserService clientUserService;
//    private final ClientUserRepository clientUserRepository;
//    private final PasswordEncoder passwordEncoder;
//    private final PushDeviceTokenService pushDeviceTokenService;
//
//    @Autowired
//    private UserLikeLogRepository userLikeLogRepository;
//
//
//    @Autowired
//    private QRService qrService;
//
//
//    @Autowired
//    private FeedbackRepository feedbackRepository;
//
//
//    @Autowired
//    private NewsRepository newsRepository;
//
//
//    @Autowired
//    private NotificationMessageService notificationMessageService;
//
//    @Autowired
//    private NotificationMessageRepository notificationMessageRepository;
//
//    @Autowired
//    private MessageReadRepository messageReadRepository;
//
//    @Value("${client.app.contextPath}")
//    private String urlPrefix;
//
//
//    @Autowired
//    RoleRepository roleRepository;
//
//    @Autowired
//    VCodeRepository vCodeRepository;
//
//    @Autowired
//    public ClientUserController(ClientUserService clientUserService, ClientUserRepository clientUserRepository, PasswordEncoder passwordEncoder, PushDeviceTokenService pushDeviceTokenService) {
//        this.clientUserService = clientUserService;
//        this.clientUserRepository = clientUserRepository;
//        this.passwordEncoder = passwordEncoder;
//        this.pushDeviceTokenService = pushDeviceTokenService;
//    }
//
//    @ApiIgnore
////    @ApiOperation("匿名登录")
//    @RequestMapping(value = "/loginByAnonymous", method = RequestMethod.POST)
//    public JsonResponse loginByAnonymous() {
////        logger.debug("nickname:" + nickname);
////        logger.debug("avatar:" + avatar);
////        ThirdPartyAccount.ThirdParty wx = ThirdPartyAccount.ThirdParty.WX;
//        String username = "G_" + UUID.randomUUID().toString();
//        String password = BaseUtil.getRandomString(4);
//        ClientUser user = null;
//        try {
//            user = clientUserRepository.findByOpenId(username);
//        } catch (Exception e) {
//        }
//        if (user == null) {
//            user = clientUserService.createThirdUser(username, password, null, null, null, ClientUser.RegistrationWay.GUEST_REGISTER);
//        }
//        clientUserService.changePassword(user, password);
//        return getAccessToken(user, password);
//    }
//
//    @ApiIgnore
////    @ApiOperation("微信登录")
//    @RequestMapping(value = "/loginByWX", method = RequestMethod.POST)
//    public JsonResponse loginByWX(@ApiParam("openId") @RequestParam String openId,
//                                  @ApiParam("accessToken") @RequestParam(required = false) String accessToken,
//                                  @ApiParam("nickname") @RequestParam(required = false) String nickname,
//                                  @ApiParam(value = "头像key") @RequestParam(required = false) String avatar,
//                                  @ApiParam("性别,0:Mr,1:Ms") @RequestParam(required = false) String sex
//    ) {
//        logger.debug("nickname:" + nickname);
//        logger.debug("avatar:" + avatar);
////        ThirdPartyAccount.ThirdParty wx = ThirdPartyAccount.ThirdParty.WX;
//        String username = "WX" + "_" + openId;
//        String password = openId + "_WX" + BaseUtil.getRandomString(4);
//        ClientUser user = null;
//        try {
//            user = clientUserRepository.findByOpenId(username);
//        } catch (Exception e) {
//        }
//        if (user == null) {
//            user = clientUserService.createThirdUser(username, password, sex, nickname, avatar, ClientUser.RegistrationWay.WEI_XIN_REGISTER);
//        }
//        clientUserService.changePassword(user, password);
//        return getAccessToken(user, password);
//    }
//
//    @ApiIgnore
////    @ApiOperation("facebook登录")
//    @RequestMapping(value = "/loginByFacebook", method = RequestMethod.POST)
//    public JsonResponse loginByFacebook(@ApiParam("openId") @RequestParam String openId,
//                                        @ApiParam("accessToken") @RequestParam(required = false) String accessToken,
//                                        @ApiParam("nickname") @RequestParam(required = false) String nickname,
//                                        @ApiParam(value = "头像key") @RequestParam(required = false) String avatar,
//                                        @ApiParam("性别,0:Mr,1:Ms") @RequestParam(required = false) String sex
//    ) {
//        logger.debug("nickname:" + nickname);
//        logger.debug("avatar:" + avatar);
////        ThirdPartyAccount.ThirdParty wx = ThirdPartyAccount.ThirdParty.WX;
//        String username = "FB" + "_" + openId;
//        String password = openId + "_FB" + BaseUtil.getRandomString(4);
//        ClientUser user = null;
//        try {
//            user = clientUserRepository.findByOpenId(username);
//        } catch (Exception e) {
//        }
//        if (user == null) {
//            user = clientUserService.createThirdUser(username, password, sex, nickname, avatar, ClientUser.RegistrationWay.FACEBOOK_REGISTER);
//        }
//        clientUserService.changePassword(user, password);
//        return getAccessToken(user, password);
//    }
//
//    @ApiIgnore
////    @ApiOperation("linkedin登录")
//    @RequestMapping(value = "/loginByLinkedIn", method = RequestMethod.POST)
//    public JsonResponse loginByLinkedIn(@ApiParam("openId") @RequestParam String openId,
//                                        @ApiParam("accessToken") @RequestParam(required = false) String accessToken,
//                                        @ApiParam("nickname") @RequestParam(required = false) String nickname,
//                                        @ApiParam(value = "头像key") @RequestParam(required = false) String avatar,
//                                        @ApiParam("性别,0:Mr,1:Ms") @RequestParam(required = false) String sex
//    ) {
//        logger.debug("nickname:" + nickname);
//        logger.debug("avatar:" + avatar);
////        ThirdPartyAccount.ThirdParty wx = ThirdPartyAccount.ThirdParty.WX;
//        String username = "LK" + "_" + openId;
//        String password = openId + "_LK" + BaseUtil.getRandomString(4);
//        ClientUser user = null;
//        try {
//            user = clientUserRepository.findByOpenId(username);
//        } catch (Exception e) {
//        }
//        if (user == null) {
//            user = clientUserService.createThirdUser(username, password, sex, nickname, avatar, ClientUser.RegistrationWay.LINKEDIN_REGISTER);
//        }
//        clientUserService.changePassword(user, password);
//        return getAccessToken(user, password);
//    }
//
//    @ApiIgnore
////    @ApiOperation("google登录")
//    @RequestMapping(value = "/loginByGoogle", method = RequestMethod.POST)
//    public JsonResponse loginByGoogle(@ApiParam("openId") @RequestParam String openId,
//                                      @ApiParam("accessToken") @RequestParam(required = false) String accessToken,
//                                      @ApiParam("nickname") @RequestParam(required = false) String nickname,
//                                      @ApiParam(value = "头像key") @RequestParam(required = false) String avatar,
//                                      @ApiParam("性别,0:Mr,1:Ms") @RequestParam(required = false) String sex
//    ) {
//        logger.debug("nickname:" + nickname);
//        logger.debug("avatar:" + avatar);
////        ThirdPartyAccount.ThirdParty wx = ThirdPartyAccount.ThirdParty.WX;
//        String username = "GL" + "_" + openId;
//        String password = openId + "_GL" + BaseUtil.getRandomString(4);
//        ClientUser user = null;
//        try {
//            user = clientUserRepository.findByOpenId(username);
//        } catch (Exception e) {
//        }
//        if (user == null) {
//            user = clientUserService.createThirdUser(username, password, sex, nickname, avatar, ClientUser.RegistrationWay.GOOGLE_REGISTER);
//        }
//        clientUserService.changePassword(user, password);
//        return getAccessToken(user, password);
//    }
//
//    @ApiOperation("修改用户信息")
//    @RequestMapping(value = "/updateUserInfo", method = RequestMethod.POST)
//    public JsonResponse updateUserInfo(@ApiIgnore Authentication authentication,
//                                       @ApiParam("头像") @RequestParam(required = false) String avatar,
//
//                                       @ApiParam("姓名") @RequestParam(required = false) String realName
//
//    ) {
//        ClientUser user = (ClientUser) authentication.getPrincipal();
//        ClientUser savedUser = clientUserRepository.findOne(user.getId());
//        if (!StringUtils.isEmpty(avatar)) {
//            savedUser.setAvatar(avatar);
//            savedUser.setAvatarType(ClientUser.AvatarType.OSS_TYPE);
//        }
//        if (StringUtils.isNotEmpty(realName)) {
//            savedUser.setRealName(realName);
//        }
//
//
//        savedUser.setUpdated(true);
//        return JsonResponse.successResponseWithData(clientUserRepository.save(savedUser));
//    }
//
//    @ApiOperation("患者用户登录")
//    @RequestMapping(value = "/registerUser", method = RequestMethod.POST)
//    public JsonResponse registerUser(
//            @ApiParam @RequestParam String telephone,
//            @ApiParam @RequestParam String vCode,
//            @ApiParam("是否定南 dingNan|notDingNan") @RequestParam(required = false) String nationType
////            @ApiParam("openId的code") @RequestParam(required = false) String code
////            String password
//    ) {
//        ClientUser clientUser = clientUserRepository.findByUsername(telephone);
//        if (null == clientUser) {
////            return JsonResponse.errorResponseWithError("用户已存在,请直接登录", "用户已存在,请直接登录");
//            clientUser = new ClientUser();
//            clientUser.setUsername(telephone);
//            clientUser.setTelephone(telephone);
//            clientUser.setCreationTime(new Date());
//        }
//
////        clientUser.setOpenId(WeChatUtil.getOpenId(code));
//
//        if (clientUser.getRoleNames().contains(Constants.ROLE_DOCTOR_USER)) {
//            return JsonResponse.errorResponseWithError("该手机号已注册成为医生账号，请登录医生端", "该手机号已注册成为医生账号，请登录医生端");
//        }
//        if (StringUtils.isNotEmpty(nationType)) {
//            clientUser.setNationType(NationType.valueOf(nationType));
//        }
//        Set<Role> roles = new HashSet<>();
//        roles.add(roleRepository.findByName(Constants.ROLE_NORMAL_USER));
//        clientUser.setRoles(roles);
//
//        VCode telephoneCode = vCodeRepository.findByTelephone(telephone);
//        if (vCode == null || !vCode.equals(telephoneCode.getvCode())) {
//            return JsonResponse.errorResponseWithError("验证码错误", "验证码错误");
//        }
//        clientUser.setEnabled(true);
//        clientUser.setActivateTime(new Date());
//        clientUserService.changePassword(clientUser, vCode);
////        return JsonResponse.successResponse();
//        return getAccessToken(clientUser, vCode);
//    }
//
//    @ApiOperation("医生登录")
//    @RequestMapping(value = "/loginDoctor", method = RequestMethod.POST)
//    public JsonResponse loginDoctor(
//            @ApiParam @RequestParam String telephone,
//            @ApiParam @RequestParam String vCode,
//            @ApiParam("是否定南 dingNan|notDingNan") @RequestParam(required = false) String nationType
////            @ApiParam("openId的code") @RequestParam(required = false) String code
////            String password
//    ) {
//        ClientUser clientUser = clientUserRepository.findByUsername(telephone);
//        if (null == clientUser) {
//            return JsonResponse.errorResponseWithError("用户不存在", "用户不存在");
////            clientUser=new ClientUser();
////            clientUser.setUsername(telephone);
////            clientUser.setTelephone(telephone);
////            clientUser.setEnabled(false);
//        }
//        if (StringUtils.isNotEmpty(nationType)) {
//            clientUser.setNationType(NationType.valueOf(nationType));
//        }
////        clientUser.setOpenId(WeChatUtil.getOpenId(code));
//        if (clientUser.getRoleNames().contains(Constants.ROLE_NORMAL_USER)) {
//            return JsonResponse.errorResponseWithError("该手机号已注册成为用户账号，请登录用户端", "该手机号已注册成为用户账号，请登录用户端");
//        }
//        Set<Role> roles = new HashSet<>();
//        roles.add(roleRepository.findByName(Constants.ROLE_DOCTOR_USER));
//        clientUser.setRoles(roles);
//        VCode telephoneCode = vCodeRepository.findByTelephone(telephone);
//        if (vCode == null || !vCode.equals(telephoneCode.getvCode())) {
//            return JsonResponse.errorResponseWithError("验证码错误", "验证码错误");
//        }
////        clientUser.setEnabled(true);
//        clientUser.setActivateTime(new Date());
//        clientUserService.changePassword(clientUser, vCode);
////        return JsonResponse.successResponse();
//        return getAccessToken(clientUser, vCode);
//    }
//
//
//    @ApiOperation("医生注册")
//    @RequestMapping(value = "/registerDoctor", method = RequestMethod.POST)
//    public JsonResponse registerDoctor(
//            @ApiParam("手机号") @RequestParam String telephone,
//            @ApiParam("姓名") @RequestParam String realName,
//            @ApiParam("所在医院") @RequestParam String hospitalName,
//            @ApiParam("个人照片") @RequestParam String personalPhoto,
//            @ApiParam("医疗证件") @RequestParam String medicalDocuments,
//            @ApiParam("擅长疾病") @RequestParam String goodAtDisease,
//            @ApiParam("身份证") @RequestParam(required = false) String personCardNum,
//            @ApiParam("openId的code") @RequestParam(required = false) String code
////            String password
//    ) {
//        ClientUser clientUser = clientUserRepository.findByUsername(telephone);
//        if (null != clientUser) {
//            return JsonResponse.errorResponseWithError("手机号已存在", "手机号已存在");
//        }
//        clientUser = new ClientUser();
//        Set<Role> roles = new HashSet<>();
//        roles.add(roleRepository.findByName(Constants.ROLE_DOCTOR_USER));
//        clientUser.setRoles(roles);
//        clientUser.setTelephone(telephone);
//        clientUser.setUsername(telephone);
//        clientUser.setRealName(realName);
//        clientUser.setCreationTime(new Date());
//        clientUser.setHospitalName(hospitalName);
//        clientUser.setPersonalPhoto(personalPhoto);
//        clientUser.setMedicalDocuments(medicalDocuments);
//        clientUser.setGoodAtDisease(goodAtDisease);
//        clientUser.setAudit("auditIng");
//        clientUser.setEnabled(false);
//        clientUser.setPassword(passwordEncoder.encode(Constants.INIT_PASSWORD));
//        clientUser.setUpdated(true);
//        clientUser.setPersonCardNum(personCardNum);
//        clientUser.setOpenId(WeChatUtil.getOpenId(code));
//        clientUserRepository.save(clientUser);
//        return JsonResponse.successResponse();
//    }
//
//
//    @ApiOperation("保存openId的code")
//    @RequestMapping(value = "/saveOpenId", method = RequestMethod.GET)
//    @ApiResponses({
//            @ApiResponse(code = 200, message = "data", response = Boolean.class)
//    })
//    public JsonResponse saveOpenId(Principal principal,
//                                   @ApiParam("openId的code") @RequestParam(required = false) String code) {
//        ClientUser clientUser = clientUserRepository.findByUsername(principal.getName());
//        clientUser.setOpenId(WeChatUtil.getOpenId(code));
//        clientUserRepository.save(clientUser);
//        return JsonResponse.successResponse();
//    }
//
//    @ApiOperation("获取是否需要补充资料接口 data 为 true 需要补充 false 不需要补充")
//    @RequestMapping(value = "/needUpdate", method = RequestMethod.GET)
//    @ApiResponses({
//            @ApiResponse(code = 200, message = "data", response = Boolean.class)
//    })
//    public JsonResponse needUpdate(@ApiIgnore Authentication authentication) {
//        ClientUser clientUser = (ClientUser) authentication.getPrincipal();
//        if (clientUser.isUpdated()) {
//            return JsonResponse.successResponseWithData(false);
//        } else {
//            return JsonResponse.successResponseWithData(true);
//        }
//    }
//
//    @ApiOperation("获取用户信息")
//    @RequestMapping(value = "/info", method = RequestMethod.GET)
//    @ApiResponses({
//            @ApiResponse(code = 200, message = "data", response = ClientUser.class)
//    })
//    public JsonResponse getUserInfo(Principal principal) {
//        ClientUser clientUser = clientUserRepository.findByUsername(principal.getName());
//        return JsonResponse.successResponseWithData(clientUser);
//    }
//
//
//
//    @ApiOperation("获取指定用户信息")
//    @RequestMapping(value = "/getByClientUserId", method = RequestMethod.GET)
//    @ApiResponses({
//            @ApiResponse(code = 200, message = "data", response = ClientUser.class)
//    })
//    public JsonResponse getByClientUserId(Principal principal,
//                                          @ApiParam("用户对象Id") @RequestParam Long clientUserId) {
//        ClientUser clientUser = clientUserRepository.findOne(clientUserId);
//        return JsonResponse.successResponseWithData(clientUser);
//    }
//
//    @ApiIgnore
//    @RequestMapping(value = "/editNickname", method = RequestMethod.POST)
//    public JsonResponse editNickname(Principal principal,
//                                     String nickname,
//                                     String avatar) {
//        ClientUser clientUser = clientUserRepository.findByUsername(principal.getName());
//        if (!StringUtils.isEmpty(nickname)) {
//            clientUser.setRealName(nickname);
//        }
//        if (!StringUtils.isEmpty(avatar)) {
//            clientUser.setAvatar(avatar);
//        }
//        clientUserRepository.save(clientUser);
//        return JsonResponse.successResponseWithData(clientUser);
//    }
//
//    @ApiOperation("修改密码")
//    @RequestMapping(value = "/modifyPwd", method = RequestMethod.POST)
//    public JsonResponse modifyPassword(Principal principal,
//                                       @ApiParam(value = "旧密码", required = true) @RequestParam("oldPwd") String oldPwd,
//                                       @ApiParam(value = "新密码", required = true) @RequestParam("newPwd") String newPwd) {
//        ClientUser clientUser = clientUserRepository.findByUsername(principal.getName());
//        if (passwordEncoder.matches(oldPwd, clientUser.getPassword())) {
//            clientUser.setPassword(passwordEncoder.encode(newPwd));
//            clientUserRepository.save(clientUser);
//            return JsonResponse.successResponse();
//        } else {
//            return JsonResponse.errorResponseWithError("1005", "failed,Original password error");
//        }
//    }
//
//
//    @ApiOperation("注册推送token")
//    @RequestMapping(value = "/registerToken", method = RequestMethod.POST)
//    public JsonResponse getSalespersonList(Principal principal,
//                                           @ApiParam(value = "友盟token", required = true) String deviceToken,
//                                           @ApiParam(value = "操作系统[iOS|Android]", required = true) String osType) {
//        ClientUser clientUser = clientUserRepository.findByUsername(principal.getName());
//        pushDeviceTokenService.registerToken(clientUser, deviceToken, PushDeviceToken.DeviceOS.valueOf(osType));
//        return JsonResponse.successResponse();
//    }
//
//    @ApiOperation("意见反馈")
//    @RequestMapping(value = "/addFeedback", method = RequestMethod.POST)
//    public JsonResponse addFeedback(Principal principal,
//                                    @ApiParam(value = "意见", required = true) String suggestion) {
//        ClientUser clientUser = clientUserRepository.findByUsername(principal.getName());
//        Feedback feedback = new Feedback();
//        feedback.setCreationTime(new Date());
//        feedback.setClientUser(clientUser);
//        feedback.setSuggestion(suggestion);
//        feedbackRepository.save(feedback);
//        return JsonResponse.successResponse();
//
//    }
//
//
//    @ApiIgnore
//    @RequestMapping(value = "/like", method = RequestMethod.POST)
//    public JsonResponse like(Principal principal,
//                             @RequestParam(required = false) Long contentId) {
//        ClientUser me = clientUserRepository.findByUsername(principal.getName());
//
//        if (null != contentId) {
//            List<UserLikeLog> userLikeLogs = userLikeLogRepository.findByClientUserAndContentId(me, contentId);
//            if (null != userLikeLogs && userLikeLogs.size() > 0) {
//                return JsonResponse.errorResponseWithError("", "you have already like");
//            }
//
//            UserLikeLog userLikeLog = new UserLikeLog();
//            userLikeLog.setClientUser(me);
//            userLikeLog.setContentId(contentId);
//            userLikeLogRepository.save(userLikeLog);
//
//            News news = newsRepository.findOne(contentId);
//            news.setLikeNum(news.getLikeNum() + 1);
//            newsRepository.save(news);
//        }
//        return JsonResponse.successResponse();
//    }
//
//    @ApiIgnore
//    @RequestMapping(value = "/unlike", method = RequestMethod.POST)
//    public JsonResponse unlike(Principal principal,
//
//                               @RequestParam(required = false) Long contentId) {
//        ClientUser me = clientUserRepository.findByUsername(principal.getName());
//        if (null != contentId) {
//            List<UserLikeLog> userLikeLogs = userLikeLogRepository.findByClientUserAndContentId(me, contentId);
//
//            if (null != userLikeLogs && userLikeLogs.size() > 0) {
//                News news = newsRepository.findOne(contentId);
//                news.setLikeNum(news.getLikeNum() - 1);
//                newsRepository.save(news);
//            }
//
//            userLikeLogRepository.delete(userLikeLogs);
//        }
//        return JsonResponse.successResponse();
//    }
//
//    @ApiIgnore
//    @RequestMapping(value = "/getMessages", method = RequestMethod.GET)
//    public JsonResponse getMessages(Principal principal, Pageable pageable) throws InvocationTargetException, IllegalAccessException {
//        ClientUser clientUser = clientUserRepository.findByUsername(principal.getName());
//        Page<NotificationMessage> notificationMessages = notificationMessageService.queryNotificationMessage(clientUser, pageable);
//        List<NotificationMessageResponse> notificationMessageResponses = new ArrayList<>();
//        if (null != notificationMessages.getContent() && notificationMessages.getContent().size() > 0) {
//            for (NotificationMessage notificationMessage : notificationMessages.getContent()) {
//                NotificationMessageResponse notificationMessageResponse = NotificationMessageResponse.turnNotificationMessageToResponse(notificationMessage);
//                List<MessageRead> messageReads = messageReadRepository.findByClientUserAndMessage(clientUser, notificationMessage);
//                if (null != messageReads && messageReads.size() > 0) {
//                    notificationMessageResponse.setRead(true);
//                } else {
//                    notificationMessageResponse.setRead(false);
//                }
//                notificationMessageResponses.add(notificationMessageResponse);
//            }
//        }
//        return JsonResponse.successResponseWithPageData(new PageImpl(notificationMessageResponses, pageable, notificationMessages.getTotalElements()));
//    }
//
//    @ApiIgnore
//    @RequestMapping(value = "/deleteMessage", method = RequestMethod.POST)
//    public JsonResponse getMessage(Principal principal, Long messageId) throws InvocationTargetException, IllegalAccessException {
//        ClientUser clientUser = clientUserRepository.findByUsername(principal.getName());
//        NotificationMessage notificationMessage = notificationMessageRepository.findOne(messageId);
//        Set<ClientUser> clientUsers = notificationMessage.getClientUsers();
//        ClientUser deleteClientUser = null;
//        for (ClientUser set : clientUsers) {
//            if (Objects.equals(set.getId(), clientUser.getId())) {
//                deleteClientUser = set;
//            }
//        }
//        clientUsers.remove(deleteClientUser);
//        notificationMessage.setClientUsers(clientUsers);
//        notificationMessageRepository.save(notificationMessage);
//        return JsonResponse.successResponse();
//    }
//
//    @ApiIgnore
//    @RequestMapping(value = "/readMessage", method = RequestMethod.POST)
//    public JsonResponse readMessage(Principal principal, Long messageId) throws InvocationTargetException, IllegalAccessException {
//        ClientUser clientUser = clientUserRepository.findByUsername(principal.getName());
//        NotificationMessage notificationMessage = notificationMessageRepository.findOne(messageId);
//        MessageRead messageRead = new MessageRead();
//        messageRead.setClientUser(clientUser);
//        messageRead.setMessage(notificationMessage);
//        messageRead.setCreationTime(new Date());
//        messageReadRepository.save(messageRead);
//        return JsonResponse.successResponse();
//    }
//
//    private JsonResponse getAccessToken(ClientUser user, String password) {
//        if (null == user || user.isDeleted()) {
//            return JsonResponse.errorResponseWithError("用户不存在", "用户不存在");
//        }
//        String result = oauthLogin(user.getUsername(), password);
//        if (!user.isEnabled()) {
//            return JsonResponse.errorResponseWithError("用户未激活", "用户未激活");
//        }
//        logger.info("登录结果:" + result);
//        try {
//            Map tokenData = new ObjectMapper().readValue(result, Map.class);
//            Map<String, Object> data = new HashMap<>();
//            if (null != tokenData.get("error")) {
//                data.put("token", tokenData);
//                return JsonResponse.errorResponseWithError("用户名密码错误", "用户名密码错误");
//            }
//            data.put("token", tokenData);
//            data.put("user", user);
//            return JsonResponse.successResponseWithData(data);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return JsonResponse.errorResponseWithError("1000", "登录失败,请重试");
//    }
//
//    private String oauthLogin(String username, String password) {
//        HttpClient httpclient = HttpClientBuilder.create().build();
//        String encoding = oauthBasicAuthBase64();
//        HttpPost httppost = new HttpPost(urlPrefix + "/oauth/token?username=" + username
//                + "&password=" + password + "&grant_type=password");
//        httppost.setHeader("Authorization", "Basic " + encoding);
//        httppost.setHeader("Accept", "application/json");
//        httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
//        HttpResponse response = null;
//        try {
//            response = httpclient.execute(httppost);
//            HttpEntity entity = response.getEntity();
//            return EntityUtils.toString(entity);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//    private static String oauthBasicAuthBase64() {
//        return org.apache.commons.codec.binary.Base64.encodeBase64String((Constants.clientID + ":" + Constants.clientSecret).getBytes());
//    }
//
//    Logger logger = LoggerFactory.getLogger(ClientUserController.class);
//}
